package nicg.st.util;

import java.util.List;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

import nicg.st.entity.UnitXmlParseBean;
import nicg.st.entity.UnitXmlCommElementBean;

import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.Document;
import org.dom4j.DocumentException;

public class UnitXmlParse {
    private static List<UnitXmlParseBean> UnitXmlParseBeanList = null;
    private static UnitXmlParseBean UnitXmlParse = null;

    private static void getIpParseByRoot(Element rootElement) {
        Iterator iterator0 = rootElement.elementIterator();
        UnitXmlParse = new UnitXmlParseBean();
        while(iterator0.hasNext()){
            Element ele1 = (Element)iterator0.next();

            if(ele1.getName().equals("ID")) {
            	String id = ele1.getStringValue();
            	UnitXmlParse.setID(id);
            }else if(ele1.getName().equals("Name")){
                String name = ele1.getStringValue();
                UnitXmlParse.setName(name);
            }else if(ele1.getName().equals("Description")) {
            	String description = ele1.getStringValue();
            	UnitXmlParse.setDescription(description);
            }else if(ele1.getName().equals("CommElement")){
                Iterator iterator1 = ele1.elementIterator();
                UnitXmlCommElementBean comElement = new UnitXmlCommElementBean();
                while(iterator1.hasNext()){
                    Element ele2 = (Element)iterator1.next();    
                    
                    if(ele2.getName().equals("BDNM")) {
                    	String bdnm = ele2.getStringValue();
                    	comElement.setBDNM(bdnm);
                    }else if(ele2.getName().equals("CMDPOSTID")) {
                    	String cmdpostid = ele2.getStringValue();
                    	comElement.setCMDPOSTID(cmdpostid);
                    }else if(ele2.getName().equals("IP")) {
                    	String ip = ele2.getStringValue();
                    	comElement.setIP(ip);
                    }else if(ele2.getName().equals("JB")) {
                    	String jb = ele2.getStringValue();
                    	comElement.setJB(jb);
                    }else if(ele2.getName().equals("JDLX")) {
                    	String jdlx = ele2.getStringValue();
                    	comElement.setJDLX(jdlx);
                    }else if(ele2.getName().equals("UnitName")) {
                    	String unitName = ele2.getStringValue();
                    	comElement.setUnitName(unitName);
                    }else if(ele2.getName().equals("VMF")) {
                    	String vmf = ele2.getStringValue();
                    	comElement.setVMF(vmf);
                    	UnitXmlParse.setCommElement(comElement);
                    	UnitXmlParseBeanList.add(UnitXmlParse);
                    }
                }
            }else if(ele1.getName().equals("Unit")){
                getIpParseByRoot(ele1);
            }
        }

    }

    public static List<UnitXmlParseBean> getParse(File file) {
        SAXReader reader = new SAXReader();

        try {
            Document document = reader.read(file);
            Element UnitXmlParseBeanstore = document.getRootElement();

            UnitXmlParseBeanList = new ArrayList<UnitXmlParseBean>();

            getIpParseByRoot(UnitXmlParseBeanstore);
        }catch(DocumentException e) {
            e.printStackTrace();
        }

        return UnitXmlParseBeanList;
    }

    
}
